import React from 'react';
import DataTable from 'components/DataTable';
import Icon from 'components/Icon';
import Button from 'components/Button';
import { router } from 'dva';
const { Link } = router;

export default (self) => [
  {
    title: 'ID',
    name: 'id',
    formItem: {
      type: 'hidden',
    }
  },
  {
    title: '用户名',
    name: 'realname',
    searchItem: {group: 'abc'},
    tableItem: {},
    formItem: {
      rules: [
        {required: false, message: '请填写用户名'},
        {validator: (rule, value, callback)=>{
            if ( !(/^[\u4e00-\u9fa5]+$/.test(value))){
              callback('请填写正确的姓名(仅可使用汉字)');
            }else{
              callback();
            }
          }}
      ],
    },
  },{
    title: '昵称',
    name: 'nickname',
    tableItem: {},
    formItem: {},
    searchItem: {}
  },{
    title: '性别',
    name: 'gender',
    dict: [{ code: 0, codeName: '女' }, { code: 1, codeName: '男' }],
    tableItem: { render: t => {if ( t !== null ) {return t?'男':'女'}}},
    formItem: {
      type: 'select',
      rules: [
        {required: false},
      ]
    }
  },
  {
    title: '手机号',
    name: 'mobile',
    searchItem: {group: 'abc'},
    tableItem: {},
    formItem: {
      rules: [
        {required: false},
        {
          validator: (rule, value, callback)=>{
            if (!value || !(/^1[3456789]\d{9}$/.test(value))){
              callback('请填写正确的手机号');
            }else{
              callback();
            }
          }
        }
      ]
    },
  },
  {
    title: '密码',
    name: 'password',
    formItem: {
      type: 'password',
      repeat: true,
      rulesForAdd: [{
        required: false}
      ],
      rules: [{required: false}]
    }
  },
  {
    title: '状态',
    name: 'isActive',
    dict: [{ code: 0, codeName: '停用' }, { code: 1, codeName: '正常' }],
    tableItem: { render: t => t?  '正常' :'停用'},
  },
  { title: '照片',
    name: 'avatar',
    formItem: {
      type: 'upload',
      action: '/api/lesson/upload/lessonCover',
      fileName: 'file',
      fileTypes: ['.png', '.jpg', '.gif'],
      listType: 'picture',
      normalize: value => {
        return [
          {
            uid: -1,
            thumbUrl: value
          }
        ];
      },
      max:1,
      multiple: false,
      rules: [
        {
          required: false,
          message: '请上传照片'
        }
      ]
    }
  },
  {
    name: 'description',
    title: '简介',
    formItem: {
      type: 'textarea',
      max:100,
      rules: [
        {required: false,message:'请控制在100字以内'},
        
        // {
        //   validator: (rule, value, callback)=>{
        //     // if (!value || !(/.{1,101}/.test(value))){
        //     if(value.length > 100){
        //       callback('请控制在100字以内');
        //     }else{
        //       callback();
        //     }
        //   }
        // },
      ]
    }
  },
  {
    title: '操作',
    tableItem: {
      width: 180,
      render: (text, record) => {
        let activeBtn;
        if (record.isActive)
          activeBtn = <Button tooltip="停用" onClick={e =>self.handleActive(record, 0)}><Icon type="StopOutlined" antd></Icon> </Button>;
        else
          activeBtn = <Button tooltip="启用" onClick={e =>self.handleActive(record, 1)}><Icon type="CheckCircleOutlined" antd></Icon> </Button>;

        return <DataTable.Oper>
          <Button tooltip="修改" onClick={e => self.onUpdate(record)}>
            <Icon type="edit" />
          </Button>
          <Button tooltip="删除" onClick={e => self.onDelete(record)}>
            <Icon type="trash" />
          </Button>
          {activeBtn}
        </DataTable.Oper>
      }
    }
  }
];
